<?php


namespace Api;


class Bootstrap
{
    public function initRoute()
    {
        try {
            //获取类名与方法
            $uri = parse_url($_SERVER['PATH_INFO']);
            $query = $uri['path'];
            $pathInfo = array_values(array_filter(explode('/', $query)));
            $className = ucfirst(isset($pathInfo[0]) ? $pathInfo[0] : '');
            $methodName = isset($pathInfo[1]) ? $pathInfo[1] : '';
            if ((!$className && $methodName) || ($className && !$methodName)) {
                throw new \Exception();
            }
            $methodNameArr = explode('_', $methodName);
            $method = '';
            foreach ($methodNameArr as $key => $value) {
                if ($key != 0) {
                    $method .= ucfirst($value);
                } else {
                    $method = $value;
                }
            }
            //获取查询参数
            if (!$className && !$methodName) {
                $classObj = new LoginController();
                $method = 'login';
            } else {
                $className .= 'Controller';
                $className = "Api\\$className";
                $classObj = new $className();
            }
            $classObj->$method();
//            exit($data);
        } catch (\Exception $e) {
            Header("HTTP/1.0 404 Not Found");
        }
    }
}